﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tank : MonoBehaviour
{
    public float moveSpeed;
    public float roundSpeed;

    //子弹预设体
    public GameObject bulletObj;
    //发射位置
    public Transform[] shootPos;
    //间隔开火时间
    public float fireOffsetTime = 1;
    //生命值
    public int hp = 100;
    // Start is called before the first frame update
    private bool isMoving = false;
    private Vector3 moveDir;

    void Start()
    {
        
    }

    public void Fire()
    {
        for (int i = 0; i < shootPos.Length; i++)
        {
            if (GameDataMgr.Instance.musicData.soundEffect)
            {
                //播放一个音效
                AudioSource source = this.gameObject.AddComponent<AudioSource>();
                //设置音效切片文件
                source.clip = Resources.Load<AudioClip>("Sound/CannonShoot");
                source.volume = GameDataMgr.Instance.musicData.volume;
                source.Play();
                Destroy(source, 2);
            }
            //实例化子弹
            Instantiate(bulletObj, shootPos[i].position, shootPos[i].rotation);
        }
    }

    public void CostHp(int hp)
    {
        this.hp += hp;
        Debug.LogFormat("坦克HP:{0}", this.hp);
    }

    public void Move(Vector3 dir)
    {
        moveDir.x = dir.x;
        moveDir.z = dir.y;
        isMoving = true;
    }

    public void StopMove()
    {
        isMoving = false;
    }

    void Update()
    {
        if (isMoving)
        {
            //位移
            this.transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
            //旋转
            this.transform.rotation = Quaternion.Lerp(this.transform.rotation, Quaternion.LookRotation(moveDir), roundSpeed * Time.deltaTime);
        }
    }
}
